﻿
Partial Class WebForm_ManageRole_OpenManageRoleHierarchyMapping
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack() Then
            Me.BindViewPoint()
            Me.InitDataAndShow()
        End If
    End Sub
    Protected Sub btQuery_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btQuery.Click
        btnApply.Visible = True
        Me.AuthorityATab.Visible = True
        Me.AuthorityBTab.Visible = True
        Me.BindHierarchy()
    End Sub
    Protected Sub btnApply_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnApply.Click
        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        ProdHierarchy = ""
        For Each node As TreeNode In tvHierarchy.CheckedNodes
            If node.Value <> "null" Then
                ProdHierarchy &= node.Value & ","

            End If
        Next

        For Each node As TreeNode In tvIndependent.CheckedNodes
            If node.Value <> "null" Then
                Select Case FirstString
                    Case "Nuance"
                        ProdHierarchy &= New String("X", 8) & node.Value & New String("X", 8) & ","
                    Case "Axe"
                        ProdHierarchy &= New String("X", 10) & node.Value & New String("X", 6) & ","
                    Case "Sub-Axe"
                        ProdHierarchy &= New String("X", 12) & node.Value & New String("X", 4) & ","
                    Case "Class"
                        ProdHierarchy &= New String("X", 14) & node.Value & New String("X", 2) & ","
                    Case "Function"
                        ProdHierarchy &= New String("X", 16) & node.Value & ","

                End Select
            End If
        Next

        '去除最後的逗號
        If ProdHierarchy.EndsWith(",") = True Then
            ProdHierarchy = ProdHierarchy.Substring(0, ProdHierarchy.Length - 1)
        End If

        sql.Length = 0
        sql.AppendFormat("delete from RoleHierarchyMapping ")
        sql.AppendFormat("where RoleID='{0}' ", Me.RoleID)
        sql.AppendFormat("and ViewPoint='{0}' ;", ViewPoint)
        If ProdHierarchy <> "" Then
            For Each PHString As String In ProdHierarchy.Split(",")
                sql.AppendFormat("insert into RoleHierarchyMapping(RoleID,ProdHierarchy,ViewPoint) ")
                sql.AppendFormat("Select ")
                sql.AppendFormat("'{0}' ", Me.RoleID)
                sql.AppendFormat(",'{0}' ", PHString)
                sql.AppendFormat(",'{0}' ", ViewPoint)
                sql.AppendFormat("where not exists ")
                sql.AppendFormat("( ")
                sql.AppendFormat("  select top 1 RoleID from RoleHierarchyMapping ")
                sql.AppendFormat("  where 1=1 ")
                sql.AppendFormat("  and RoleID='{0}' ", Me.RoleID)
                sql.AppendFormat("  and ProdHierarchy='{0}' ", PHString)
                sql.AppendFormat("  and ViewPoint='{0}' ", ViewPoint)
                sql.AppendFormat(") ")
                sql.AppendFormat("; ")
            Next
        End If
        dat.AccData(sql.ToString())
        MyObj.ShowMessage("Apply Successful!")
    End Sub


#Region "方法"
    Public Sub InitDataAndShow()
        RoleID = Me.Request.QueryString("RoleID")
        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        sql.AppendFormat("Select * from Role ")
        sql.AppendFormat("where RoleID = '{0}' ", RoleID)

        Division = dat.GetData(sql).Rows(0)("Division").ToString()


    End Sub
#End Region

#Region "繫結資料"

    Dim MyDT As DataTable
    Dim MyInDT As DataTable
    Dim MaxCount As Integer
    '處理已設定checkbox預勾
    Dim HiDT As Data.DataTable
    Dim HiInDT As Data.DataTable
    Private Sub BindHierarchy()
        Me.tvHierarchy.Attributes.Add("onClick", "OnCheckEvent()")
        Me.tvIndependent.Attributes.Add("onClick", "OnCheckEvent()")
        Me.tvHierarchy.Visible = True
        Me.tvHierarchy.Nodes.Clear()
        Me.tvIndependent.Visible = True
        Me.tvIndependent.Nodes.Clear()

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("Select ProdHierarchy from RoleHierarchyMapping ")
        sql.AppendFormat("where RoleID = '{0}' ", RoleID)
        sql.AppendFormat("and ViewPoint= '{0}' ", ddlViewPoint.SelectedValue)
        HiDT = dat.GetData(sql)
        HiInDT = HiDT

        '取前四階
        HiDT.DefaultView.RowFilter = "len(ProdHierarchy)<=8"
        HiDT = HiDT.DefaultView.ToTable()
        HiDT.PrimaryKey = New DataColumn() {HiDT.Columns(0)}


        '取獨立階層
        HiInDT.DefaultView.RowFilter = "len(ProdHierarchy)=18"
        HiInDT = HiInDT.DefaultView.ToTable()
        HiInDT.PrimaryKey = New DataColumn() {HiInDT.Columns(0)}


        sql.Length = 0
        sql.AppendFormat("Select q.HierarchyName,a.Chain,a.Sequence ")
        sql.AppendFormat("from (Select top 3 HierarchyName from ")
        sql.AppendFormat("MaintainLevel ")
        sql.AppendFormat("Where IsSelect = 'Y' ")
        sql.AppendFormat("and Division = '{0}' ", Division)
        sql.AppendFormat("and ViewPoint = '{0}' ", ddlViewPoint.SelectedValue)
        sql.AppendFormat("order by Sequence  ")
        sql.AppendFormat(") q ")
        sql.AppendFormat("join  ProductLevel a on q.HierarchyName=a.LevelName ")
        sql.AppendFormat("order by a.Sequence ")
        MyDT = dat.GetData(sql)
        MyInDT = MyDT

        '取出前四階
        MyDT.DefaultView.RowFilter = "Chain='Y'"
        MyDT = MyDT.DefaultView.ToTable()

        If MyDT.Rows.Count > 0 Then
            MaxCount = MyDT.Rows.Count - 1
            Dim node As New TreeNode

            node.Text = MyDT.Rows(0)("HierarchyName").ToString()
            node.Value = "null"
            If HiDT.Rows.Count > 0 Then
                node.Checked = True
            End If
            Me.tvHierarchy.Nodes.Add(node)
            Me.BindHierarchy(node, 0)
            Me.tvHierarchy.CollapseAll()

        End If


        '取出獨立階層
        MyInDT.DefaultView.RowFilter = "Chain='N'"
        MyInDT = MyInDT.DefaultView.ToTable()

        If MyInDT.Rows.Count > 0 Then
            Dim node As New TreeNode

            node.Text = MyInDT.Rows(0)("HierarchyName").ToString()
            node.Value = "null"
            If HiInDT.Rows.Count > 0 Then
                node.Checked = True
            End If
            FirstString = MyInDT.Rows(0)("HierarchyName").ToString()
            Me.tvIndependent.Nodes.Add(node)
            Me.BindIndependent(node, 0)
            Me.tvIndependent.CollapseAll()

        End If

    End Sub
    '處理前四階
    Private Sub BindHierarchy(ByVal node As TreeNode, ByVal RowIndex As Integer)
        If RowIndex > MaxCount Then Return

        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New Data.DataTable
        Dim dtTree As New Data.DataTable

        sql.Length = 0
        If RowIndex = 0 Then
            sql.AppendFormat("select a.Description,a.Code from ProductHierarchyOriginal a  ")
            sql.AppendFormat("where a.LevelName='{0}' ", node.Text)
            sql.AppendFormat("and a.Code in (select Brand from DivisionBrandMappingView where Division ='{0}') ", Division)
            sql.AppendFormat("order by a.Description ")
        Else
            sql.AppendFormat("select distinct a.Description,a.Code from ProductHierarchyOriginal a ")
            sql.AppendFormat("join ProductLevel b ")
            sql.AppendFormat("on a.LevelName=b.LevelName ")
            sql.AppendFormat("and b.LevelName='{0}' ", MyDT.Rows(RowIndex)("HierarchyName").ToString())
            sql.AppendFormat("join ProductHierarchy_All al on  ")
            sql.AppendFormat("substring(a.Code,1,(b.Sequence)*2)=substring(al.prodhierarchy,1,(b.Sequence)*2) ")
            'sql.AppendFormat("where substring(a.Code,1,(b.Sequence-1)*2)='{0}' ", node.Value)
            sql.AppendFormat("where Left(a.Code,len('{0}'))='{0}' ", node.Value)
            sql.AppendFormat("order by  a.Description ")
        End If

        dtTree = dat.GetData(sql)

        If dtTree.Rows.Count > 0 Then
            For Each dr As Data.DataRow In dtTree.Rows
                Dim child As New TreeNode()
                child.Text = dr("Description")
                child.Value = dr("Code")
                If HiDT.Rows.Contains(dr("Code").ToString()) Then
                    child.Checked = True
                End If

                node.ChildNodes.Add(child)
                Me.BindHierarchy(child, RowIndex + 1)
            Next
        End If

    End Sub
    '處理獨立階
    Private Sub BindIndependent(ByVal node As TreeNode, ByVal RowIndex As Integer)
        If RowIndex < 0 Then Return

        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        Dim dt As New Data.DataTable
        Dim dtITree As New Data.DataTable

        sql.Length = 0
        sql.AppendFormat("select  distinct right(p.Code,2) as Code,p.Description from ")
        sql.AppendFormat("ProductHierarchy_All a ")
        sql.AppendFormat("join ChannelView c on left(a.ProdHierarchy,2)=c.Signature ")
        sql.AppendFormat("join ProductHierarchyOriginal p on ")
        sql.AppendFormat("c.Signature=left(p.Code,2) ")
        sql.AppendFormat("where c.Division='{0}' ", Division)
        sql.AppendFormat("and p.LevelName='{0}' ", node.Text)
        sql.AppendFormat("order by p.Description ")
        dtITree = dat.GetData(sql)

        If dtITree.Rows.Count > 0 Then
            For Each dr As Data.DataRow In dtITree.Rows
                Dim child As New TreeNode()
                child.Text = dr("Description")
                child.Value = dr("Code")

                Dim childSting As String = ""
                Select Case FirstString
                    Case "Nuance"
                        childSting = New String("X", 8) & child.Value & New String("X", 8)
                    Case "Axe"
                        childSting = New String("X", 10) & child.Value & New String("X", 6)
                    Case "Sub-Axe"
                        childSting = New String("X", 12) & child.Value & New String("X", 4)
                    Case "Class"
                        childSting = New String("X", 14) & child.Value & New String("X", 2)
                    Case "Function"
                        childSting = New String("X", 16) & child.Value

                End Select
                If HiInDT.Rows.Contains(childSting) Then
                    child.Checked = True
                End If

                node.ChildNodes.Add(child)
                'Me.BindIndependent(child, RowIndex + 1)
            Next
        End If
    End Sub
    Protected Sub BindViewPoint()
        ddlViewPoint.DataTextField = "Text"
        ddlViewPoint.DataValueField = "Value"

        ddlViewPoint.DataSource = MyObj.GetCodeMappingData("Product", "ViewPoint")
        ddlViewPoint.DataBind()
        ddlViewPoint.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub
#End Region

#Region "屬性"
    Public Property FirstString() As String
        Get
            Return ViewState("FirstString")
        End Get
        Set(ByVal value As String)
            ViewState("FirstString") = value
        End Set
    End Property
    Public Property ProdHierarchy() As String
        Get
            Return ViewState("ProdHierarchy")
        End Get
        Set(ByVal value As String)
            ViewState("ProdHierarchy") = value
        End Set
    End Property
    Public Property ViewPoint() As String
        Get
            Return Me.ddlViewPoint.SelectedValue
        End Get
        Set(ByVal value As String)
            Me.ddlViewPoint.SelectedValue = value
        End Set
    End Property
    Public Property Division() As String
        Get
            Return ViewState("Division")
        End Get
        Set(ByVal value As String)
            ViewState("Division") = value
        End Set
    End Property
    Public Property RoleID() As Integer
        Get
            Return ViewState("RoleID")
        End Get
        Set(ByVal value As Integer)
            ViewState("RoleID") = value
        End Set
    End Property
#End Region


End Class
